<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>E:nth-of-type(n)</title>
<meta name="description" content="CSS3参考手册之: E:nth-of-type(n)" />
<meta name="keywords" content="E:nth-of-type(n), css3选择符, css3, css3参考手册" />
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<meta name="robots" content="all" />
<!--[if lte IE 8]>
<script src="../../js/html5.js"></script>
<![endif]-->
<link rel="stylesheet" href="../../skin/article.css" />
</head>
<body>
<nav id="guide" class="g-mod">
	<div class="from">
		<a href="http://css.doyoe.com/" target="_blank">CSS3参考手册</a>
		<span>&#187;</span>
		<a href="../index.htm">选择符列表</a>
		<span>&#187;</span>
		<a href="index.htm" id="category" rel="selectors/pseudo-classes" name="nth-of-type(n)">伪类选择符</a>
		<span>&#187;</span>
	</div>
	<div class="to">
		<span class="label">相关内容：</span>
		<div class="g-combobox g-transition">
			<a href="?" class="g-transition target">
				<strong>其它伪类选择符</strong>
				<span>选择其它项<!--[if lte IE 7]><ins>IE7 and earlier, Get to die</ins><![endif]--></span>
			</a>
			<div class="g-transition list">
				<ul>
					<!-- 插入分类快速导航 -->
				</ul>
			</div>
		</div>
	</div>
</nav>
<header id="hd">
	<section id="title" class="g-mod">
		<h1 class="tit">E:nth-of-type(n)</h1>
		<ul class="info">
			<li><strong>版本：CSS3</strong></li>
		</ul>
		<!-- 插入浏览器信息 -->
	</section>
</header>
<section id="bd">
	<section id="syntax" class="g-mod g-attr">
		<h2 class="tit">语法：</h2>
		<div class="cont">
			<p><strong>E:nth-of-type(n)</strong> { sRules }</p>
		</div>
	</section>
	<section id="intro" class="g-mod g-attr">
		<h2 class="tit">说明：</h2>
		<div class="cont">
			<strong>匹配父元素的第n个子元素E。</strong>
			<p>需要注意3个要点：</p>
			<ul>
				<li>首先，因为匹配的是父元素的子元素，这意味着E元素必须作为某个元素的子元素存在（E元素的父元素最高是html，也就是说E元素最高是body，这表示任何非html的元素都符合这个约束，因为html元素是根元素。）</li>
				<li>
					<p>其次，匹配的是父元素的第n个为E的子元素（被命中的不一定是第n个子元素，因为匹配的不是第n个子元素，而是第n个为E的子元素）</p>
					<strong>来看下面这个例子：</strong>
					<div class="gquote">
						<p class="gquote-tit"><strong>HTML示例代码：</strong></p>
						<blockquote class="gquote-cont"><code><xmp><div class="demo">
	<p>p1</p>
	<p>p2</p>
	<span>span1</span>
	<p>p3</p>
	<span>span2</span>
</div></xmp></code></blockquote>
						<p class="gquote-tit"><strong>如上HTML，假设要命中第3个p元素，代码如下：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>.demo p:nth-of-type(3) { color: #f00; }</xmp></code></blockquote>
						<p class="gquote-info">可以看到此时 .demo 的第4个子元素会被命中，因为 .demo 的第4个子元素才是它的第3个p子元素</p>
					</div>
				</li>
				<li>
					<p>再次，只能匹配与E元素同级的元素，即：E元素的兄弟元素</p>
					<strong>来看下面这个例子：</strong>
					<div class="gquote">
						<p class="gquote-tit"><strong>HTML示例代码：</strong></p>
						<blockquote class="gquote-cont"><code><xmp><div class="demo">
	<span>span1</span>
	<p>
		<span>span2</span>
		<span>span3</span>
	</p>
	<span>span4</span>
</div></xmp></code></blockquote>
						<p class="gquote-tit"><strong>如上HTML，如果我想匹配其中的第2个span，代码如下：</strong></p>
						<blockquote class="gquote-cont"><code><xmp>.demo span:nth-of-type(2) { color: #f00; }</xmp></code></blockquote>
						<p class="gquote-info">结果span2确实没有被命中，因为span2并不是.demo的第2个span子元素。从代码中能看到span1和span4是同级，span2和span3同级，因为span3和span4分别是这2组层级中的第2个span，所以最终span3和span4都被命中了。</p>
					</div>
				</li>
			</ul>
		</div>
	</section>
	<section class="g-mod g-attr" id="compatible">
		<h2 class="tit">兼容性：</h2>
		<div class="cont">
			<ul class="support-type">
				<li><span class="support">浅绿</span> = 支持</li>
				<li><span class="unsupport">红色</span> = 不支持</li>
				<li><span class="partsupport">粉色</span> = 部分支持</li>
			</ul>
			<table class="g-data">
				<thead>
					<tr>
						<th>IE</th>
						<th>Firefox</th>
						<th>Chrome</th>
						<th>Safari</th>
						<th>Opera</th>
						<th>iOS Safari</th>
						<th>Android Browser</th>
						<th>Android Chrome</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td class="unsupport">6.0-8.0</td>
						<td class="support" rowspan="2">2.0+</td>
						<td class="support" rowspan="2">4.0+</td>
						<td class="support" rowspan="2">3.1+</td>
						<td class="support" rowspan="2">3.5+</td>
						<td class="support" rowspan="2">3.2+</td>
						<td class="support" rowspan="2">2.1+</td>
						<td class="support" rowspan="2">18.0+</td>
					</tr>
					<tr>
						<td class="support">IE9.0+</td>
					</tr>
				</tbody>
			</table>
		</div>
	</section>
	<section id="example" class="g-mod g-attr">
		<h2 class="tit">示例：</h2>
		<div class="cont">
			<textarea cols="90" rows="10">
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>结构性伪类选择符 E:nth-of-type(n)_CSS参考手册_web前端开发参考手册系列</title>
<meta name="author" content="Joy Du(飘零雾雨), dooyoe@gmail.com, www.doyoe.com" />
<style>
.demo p:nth-of-type(2) {
	color: #f00;
}
</style>
</head>
<body>
<div class="demo">
	<p>我是一个p元素</p>
	<div>我是一个div元素</div>
	<p>我是一个p元素</p>
	<p>我是一个p元素</p>
</div>
</body>
</html>
			</textarea>
			<p><input type="button" value="运行" class="g-btn g-btn-sure" /></p>
		</div>
	</section>
</section>
<footer id="ft">
	<aside id="rights" class="g-mod">
		<!-- 插入浏览器及版权信息 -->
	</aside>
</footer>
<script src="../../js/jquery.js"></script>
<script src="../../js/inner.js"></script>
</body>
</html>